using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace ScienceNET.ImageProcessing
{
    public partial  class ImageProcessing
    {
        public static double[,] ImageReadGray(string fileName)
        {
            
            Bitmap bmp = new Bitmap(fileName);
            double[,] result = new double[bmp.Height, bmp.Width];
            if (bmp.PixelFormat == PixelFormat.Format24bppRgb)
            {
                for (int y = 0; y < bmp.Height; y++)
                    for (int x = 0; x < bmp.Width; x++)
                        result[y, x] = bmp.GetPixel(x, y).GetBrightness();

            }
            return result;
        }
        public static Bitmap Image2Bmp(double[,] imageGray)
        {
            Bitmap bmp = new Bitmap(imageGray.GetLength(1), imageGray.GetLength(0), PixelFormat.Format24bppRgb);
            for (int y = 0; y < bmp.Height; y++)
                for (int x = 0; x < bmp.Width; x++)
                {
                    int gray=(int)(255.0*imageGray[y,x]);
                    bmp.SetPixel(x,y,Color.FromArgb(gray,gray,gray));
                }
            return bmp;
        }

        /// <summary>
        /// Converts three image matrices to a RGB bitmap.
        /// </summary>
        /// <param name="imageR">Red color matrix.</param>
        /// <param name="imageG">Green color matrix.</param>
        /// <param name="imageB">Blue color matrix.</param>
        public static void Image2Bmp(double[,] imageR, double[,] imageG, double[,] imageB)
        {
            Bitmap bmp = new Bitmap(imageR.GetLength(1), imageR.GetLength(0), PixelFormat.Format24bppRgb);
            for (int y = 0; y < bmp.Height; y++)
                for (int x = 0; x < bmp.Width; x++)
                {
                    int r = (int)(255.0 * imageR[y, x]);
                    int g = (int)(255.0 * imageR[y, x]);
                    int b = (int)(255.0 * imageR[y, x]);
                    bmp.SetPixel(x, y, Color.FromArgb(r, g, b));
                }

        }

    }

}
